#!/bin/sh

prefix="%OPTWARE_TARGET_PREFIX%"

[ -e ${prefix}/etc/default/nginx ] && . ${prefix}/etc/default/nginx

if [ "$NGINX_ENABLE" != "yes" ]; then
    exit
fi

PATH=${prefix}/bin:${prefix}/sbin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
DAEMON=${prefix}/sbin/${NAME}

test -x $DAEMON || exit 0

if [ -z "$1" ] ; then
    case `echo "$0" | sed 's:^.*/\(.*\):\1:g'` in
        S??*) rc="start" ;;
        K??*) rc="stop" ;;
        *) rc="usage" ;;
    esac
else
    rc="$1"
fi

case "$rc" in
    start)
	echo "Starting web server: $NAME"
	$DAEMON
        ;;
    stop)
        if [ -n "`pidof $NAME`" ]; then
            echo "Stopping web server: $NAME"
            killall $NAME 2> /dev/null
        fi
        ;;
    restart)
        "$0" stop
        sleep 1
        "$0" start
        ;;
    *)  
        echo "Usage: $0 (start|stop|restart|usage)"
        ;;
esac

exit 0
